package com.hippo.core.ui.compose.mine

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hippo.core.R
import com.hippo.core.config.AppStoreKey
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.core.manager.AppStore
import com.hippo.core.state.DialogType
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.KayValueLayout
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.compose.control.TempUnitDialog
import com.hippo.core.ui.compose.control.TimeFormatDialog
import com.hippo.core.ui.theme.ColorBackgroundGray
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.DeviceSettingVM

@Composable
fun DeviceSettingScreen(navController: NavController, vm: DeviceSettingVM = viewModel()) {

    val dialogType = remember { mutableStateOf<DialogType>(DialogType.Idle) }

    val uiState = vm.uiState

    LaunchedEffect(Unit) {
        val unit = AppStore.readString(AppStoreKey.TempUnit, "℉") ?: "℉"
        val timeFormat =
            AppStore.readString(AppStoreKey.TimeFormatStr, "12-hour format") ?: "12-hour format"
        vm.updateState(uiState.copy(temp = unit, timeFormat = timeFormat))
    }

    when (dialogType.value) {
        DialogType.TempUnit -> {
            TempUnitDialog(
                onDismissRequest = {
                    dialogType.value = DialogType.Idle
                },
                onSave = { name ->
                    dialogType.value = DialogType.Idle
                    vm.updateState(uiState.copy(temp = name))
                    AppStore.save(AppStoreKey.TempUnit, name)
                }
            )
        }

        DialogType.TimeFormat -> {
            TimeFormatDialog(
                onDismissRequest = {
                    dialogType.value = DialogType.Idle
                },
                onSave = { name ->
                    dialogType.value = DialogType.Idle
                    vm.updateState(uiState.copy(timeFormat = name))
                }
            )
        }

        else -> {}
    }


    CommonBody(
        title = stringResource(R.string.setting_10),
        onBack = {
            navController.popBackStack()
        }
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()

        ) {

            Box {

                Image(
                    painter = painterResource(R.mipmap.big_bg),
                    contentDescription = null, // 图片的描述，如果是背景可以设为null
                    contentScale = ContentScale.Crop, // 裁剪图片以适应屏幕
                    modifier = Modifier.fillMaxWidth()
                )

                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 10.dp, horizontal = 10.dp),
                ) {

                    KayValueLayout(
                        key = stringResource(R.string.setting_11),
                        value = uiState.temp.orEmpty(),
                        rightAlign = true,
                        showArrow = true
                    ) {
                        dialogType.value = DialogType.TempUnit
                    }

                    HorizontalDivider(thickness = 0.5.dp)

                    KayValueLayout(
                        key = stringResource(R.string.setting_12),
                        value = uiState.length.orEmpty(),
                        rightAlign = true,
                        showArrow = false
                    )
                    HorizontalDivider(thickness = 0.5.dp)

                    KayValueLayout(
                        key = stringResource(R.string.setting_13),
                        value = uiState.area.orEmpty(),
                        rightAlign = true,
                        showArrow = false
                    )
                    HorizontalDivider(thickness = 0.5.dp)
                    KayValueLayout(
                        key = stringResource(R.string.setting_14),
                        value = uiState.timeFormat.orEmpty(),
                        rightAlign = true,
                        showArrow = true
                    ) {
                        dialogType.value = DialogType.TimeFormat
                    }

                }
            }
        }
    }

}


@Preview(showBackground = true)
@Composable
private fun DeviceSettingScreenPreview() {
    HippoTheme {
        DeviceSettingScreen(
            navController = rememberNavController()
        )
    }
}